package org.kymjs.kjframe.demo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.provider.Settings;

import androidx.preference.PreferenceManager;

public class SettingsHelper {

    private static final String TAG = SettingsHelper.class.getSimpleName();
    private static final String SETTINGS_NAME_REMOTE_CONTROL = "remote_control";
    private static final String SETTING_NAME_INVITE_CODE = "invite_code";
    private static final String SETTING_NAME_DEVICE_ID = "device_id";
    private SharedPreferences sharedPreferences;
    public interface SettingsChangeListener {
        void onInviteCodeChanged(String newInviteCode);
    }

    private SettingsChangeListener settingsChangeListener;

    private final SharedPreferences.OnSharedPreferenceChangeListener preferenceChangeListener =
            (sharedPreferences, key) -> {
                if (settingsChangeListener == null) return;

                if (SETTING_NAME_INVITE_CODE.equals(key)) {
                    String newCode = sharedPreferences.getString(key, "");
                    settingsChangeListener.onInviteCodeChanged(newCode);
                }
            };

    public SettingsHelper(Context context, SettingsChangeListener listener) {
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        sharedPreferences.registerOnSharedPreferenceChangeListener(preferenceChangeListener);
        this.settingsChangeListener = listener;
        @SuppressLint("HardwareIds")
        String androidId = Settings.Secure.getString(
                context.getContentResolver(),
                Settings.Secure.ANDROID_ID
        );
        setDeviceId(androidId);
    }

    public void setSettingsChangeListener(SettingsChangeListener listener) {
        this.settingsChangeListener = listener;
    }

    public void close() {
        if (sharedPreferences != null) {
            sharedPreferences.unregisterOnSharedPreferenceChangeListener(preferenceChangeListener);
        }
        sharedPreferences = null;
    }

    private void setDeviceId(String deviceId) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(SETTING_NAME_DEVICE_ID, deviceId);
        editor.commit();
    }
    public void setRemoteControlEnabled(boolean enabled) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(SETTINGS_NAME_REMOTE_CONTROL, enabled);
        editor.commit();
    }
    public void setInviteCode(String inviteCode) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(SETTING_NAME_INVITE_CODE, inviteCode);
        editor.commit();
    }
    public boolean isRemoteControlEnabled() {
        boolean isEnabled;
        try {
            isEnabled = sharedPreferences.getBoolean(SETTINGS_NAME_REMOTE_CONTROL, false);
        } catch (Exception e) {
            Log.e(TAG, "Failed to parse remote control settings");
            isEnabled = false;
        }

        return isEnabled;
    }
    public String getInviteCode() {
        String inviteCode;
        try {
            inviteCode = sharedPreferences.getString(SETTING_NAME_INVITE_CODE, "");
        } catch (Exception e) {
            Log.e(TAG, "Failed to parse remote control settings");
            inviteCode = "";
        }
        return inviteCode;
    }
    public String getDeviceId() {
        String deviceId;
        try {
            deviceId = sharedPreferences.getString(SETTING_NAME_DEVICE_ID, "");
        } catch (Exception e) {
            Log.e(TAG, "Failed to parse remote control settings");
            deviceId = "";
        }
        return deviceId;
    }
}
